package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.constant.UserStatus;
import com.prd.mapper.EntInviteMapper;
import com.prd.model.EntInvite;
import com.prd.model.LoginUser;
import com.prd.service.EntInviteService;
import com.prd.util.UserInfoUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EntInviteServiceImpl extends ServiceImpl<EntInviteMapper, EntInvite> implements EntInviteService {

    @Autowired
    private EntInviteMapper entInviteMapper;

    @Override
    public int insertEntInvite(EntInvite entInvite) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entInvite);
        return entInviteMapper.insert(entInvite);
    }

    @Override
    public int updateEntInvite(EntInvite entInvite) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entInvite);
        return entInviteMapper.updateById(entInvite);
    }

    @Override
    public List<EntInvite> external(String search) {
        LambdaQueryWrapper<EntInvite> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(EntInvite::getAttendee, search + "%");
        lambdaQueryWrapper.or().like(EntInvite::getPhone, search + "%");
        lambdaQueryWrapper.eq(EntInvite::getDelFlag, UserStatus.OK.getCode());
        return entInviteMapper.selectList(lambdaQueryWrapper);
    }
}
